<?php
/**
 * bootstrap.php            引导文件
 *
 * @author                  mashanling <msl-138@163.com>
 * @date                    2012-12-24 14:39:28
 * @lastmodify              2013-01-22 13:27:08 by mashanling
 */


!defined('WWWROOT') && exit('Access Denied');

define('IS_LOCAL'              , true);     //是否本地环境
define('APP_DEBUG'             , true);     //调试
define('RUNTIME_FILE'          , __DIR__ . '/runtime.php');//运行时文件

!extension_loaded('yaf') && exit('yaf extension required!');
!ini_get('yaf.use_namespace') && exit('yaf.use_namespace=on required!');
!version_compare(PHP_VERSION, '5.3', '>') && exit('php5.3 or higher required!');

ob_get_level() != 0 && ob_end_clean();

header('content-type: text/html; charset=utf-8');

if (APP_DEBUG || !is_file(RUNTIME_FILE)) {
    build_runtime_file();

    if (APP_DEBUG && is_file(RUNTIME_FILE)) {
        unlink(RUNTIME_FILE);
    }
}
else {
    require(RUNTIME_FILE);
}

$configs = array(
    'application' => array(
        'directory'  => APP_PATH,
        'bootstrap'  => APP_PATH . 'Bootstrap.php',
        'ext'        => 'class.php',
        'dispatcher' => array(
            'throwException' => 1,
            'catchException' => 0,
        ),
    ),
);

$app = new Yaf\Application($configs);
$app->getDispatcher()->registerPlugin(get_instance_of('AppPlugin'));
$app->bootstrap()->run();

/**
 * 创建运行时文件
 *
 * @author          mashanling <msl-138@163.com>
 * @date            2012-12-30 13:56:04
 * @lastmodify     2012-12-30 13:56:04 by mashanling
 *
 * @return void 无返回值
 */
function build_runtime_file() {
    $filesize      = 0;//加载文件大小
    $compile       = "<?php\n!defined('WWWROOT') && exit('Access Denied');";//编译内容
    $require_files = array(
        INCLUDE_PATH . 'functions.php',
        INCLUDE_PATH . 'app_config.php',
        INCLUDE_PATH . 'db_config.php',
    );

    foreach ($require_files as $file) {
        require($file);

        if (!APP_DEBUG) {
            $filesize += filesize($file);
            $compile  .= compile_file($file);
        }
    }

    $require_files = array(
        LIB_PATH . 'AppPlugin.php',
        LIB_PATH . 'functions/time.php',
        LIB_PATH . 'Filter.php',
        LIB_PATH . 'Db.php',
        LIB_PATH . 'Db' . ucfirst(DB_TYPE) . '.php',
        LIB_PATH . 'Model.php',
        LIB_PATH . 'Log.php',
        APP_PATH . 'controllers/Base.php',
        APP_PATH . 'models/Base.php',
    );

    foreach ($require_files as $file) {
        require($file);

        if (!APP_DEBUG) {
            $filesize += filesize($file);
            $compile  .= compile_file($file);
        }
    }

    if (!APP_DEBUG) {
        file_put_contents(RUNTIME_FILE, $compile);
        $size = filesize(RUNTIME_FILE);//编译后大小
        file_put_contents('built_runtime_file.log', local_date() . '(' . format_size($filesize) . ' => ' . format_size($size) . ')' . EOL_LF, FILE_APPEND);
    }

}

/**
 * 获取php文件内容，并去掉注释及空白
 *
 * @author          mashanling <msl-138@163.com>
 * @date            2012-12-30 14:00:11
 * @lastmodify     2013-01-02 16:27:35 by mashanling
 *
 * @return string 去掉注释及空白后php代码
 */
function compile_file($filename) {
    $content = substr(php_strip_whitespace($filename), 7);

    if (strpos($content, '?>') && '?>' == substr($content = rtrim($content), -2)) {//php关闭标签
        $content = substr($content, -2);
    }

    return  "\n\n//{$filename}\n" . $content;
}